﻿#include "CPdfRptDataPkg.h"
#include "CGlobal.h"
#include "CSqlOper.h"
#include "Public.h"

CPdfRptDataPkg::CPdfRptDataPkg(QObject *parent)
	: QObject(parent)
{

}

CPdfRptDataPkg::~CPdfRptDataPkg()
{

}

bool CPdfRptDataPkg::pkgRestAiPdfRptData(QDateTime dtSearchStartTime, QDateTime dtSearchEndTime, QString sPdfFolderPath, QString sHttpPrefix, CAllPdfRptDataInfo &dataInfo, QString &sErrMsg)
{
	//AI检测
	QDateTime dtCurTime = QDateTime::currentDateTime();//当前系统时间
	QString sCurrTime = dtCurTime.toString("yyyyMMddhhmmss");
	//创建文件夹
	QString sFolderTemp = sPdfFolderPath + "Result/"+"HkAi" + "/" + QDateTime::currentDateTime().toString("yyyyMM") + "/";//文件夹+RestAi+年月
	QDir folder(sFolderTemp);// 创建QDir对象
	if (!folder.exists())
	{
		// 文件夹不存在，则创建
		folder.mkpath(".");
	}
	//输出路径
	QString sOutPath = sFolderTemp + sCurrTime + A2T("AI检测报告.pdf");
	dataInfo.extraAiDataInfo.m_sOutputPdfFolder = sOutPath;

	//去查询报警记录表
	QVector<QSqlRecord> vecInfo;
	bool bRet=CSqlOper::instance().selectAlarmResult(dtSearchStartTime.toString("yyyy/MM/dd hh:mm:ss"), dtSearchEndTime.toString("yyyy/MM/dd hh:mm:ss"), vecInfo,sErrMsg);
	if (bRet == false)
	{
		return false;
	}
	dataInfo.extraAiDataInfo.m_sDetTime = dtSearchEndTime.toString("yyyy-MM-dd");
	dataInfo.extraAiDataInfo.m_sDetStartTime = dtSearchStartTime.toString("hh:mm:ss");
	dataInfo.extraAiDataInfo.m_sDetEndTime = dtSearchEndTime.toString("hh:mm:ss");
	dataInfo.extraAiDataInfo.m_nAlmCount = vecInfo.size();
	for (int i = 0; i < vecInfo.size(); i++)
	{
		QString sDevName = vecInfo[i].value("DEV_NAME").toString();
		QString sCamerIp = vecInfo[i].value("CAMER_IP").toString();
		QString sCamerUser = vecInfo[i].value("CAMER_USER").toString();
		QString sCamerPassWd = vecInfo[i].value("CAMER_PASSWD").toString();
		QString sEventType = vecInfo[i].value("EVENT_TYPE").toString();
		QString sAlarmTime = vecInfo[i].value("ALARM_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss");
		QString sAlarmLevel = vecInfo[i].value("ALARM_LEVEL").toString();
		int nConfirm = vecInfo[i].value("IS_CONFIRM").toInt();
		QString sConfirm = "";
		if (nConfirm == 0)
		{
			sConfirm = A2T("否");
		}
		else if (nConfirm == 1)
		{
			sConfirm = A2T("是");
		}
		QString sUrl = vecInfo[i].value("IMG_URL").toString();
		SPdfExtraAiAlmInfo info;
		info.sCamerIp = sCamerIp;
		info.sCamerName = sDevName;
		info.sAlmTime = sAlarmTime;
		info.sAlarmLevel = sAlarmLevel;
		info.sEventType = sEventType;
		info.sIsConfrim = sConfirm;
		info.sUrl = sUrl;
		info.sCamerUser = sCamerUser;
		info.sCamerPassWd = sCamerPassWd;
		if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(sEventType))
		{
			// 直接获取引用并修改原始向量
			dataInfo.extraAiDataInfo.m_mapExtraAiInfo[sEventType].push_back(info);
		}
		else
		{
			QVector<SPdfExtraAiAlmInfo> vecInfo;
			vecInfo.push_back(info);
			dataInfo.extraAiDataInfo.m_mapExtraAiInfo.insert(sEventType, vecInfo);
		}
	}
	//categories << A2T("堆煤煤流占比") << A2T("皮带异物") << A2T("大块煤检测") << A2T("皮带跑偏算法") << A2T("安全帽反光衣") << A2T("烟雾火点") << A2T("抽烟打电话");
	//遍历完成以后，各个报警总数更新
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("安全帽反光衣")))
	{
		dataInfo.extraAiDataInfo.m_nSafetyHelmet = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("安全帽反光衣")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("堆煤煤流占比")))
	{
		dataInfo.extraAiDataInfo.m_nCoalFlow = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("堆煤煤流占比")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("皮带异物")))
	{
		dataInfo.extraAiDataInfo.m_nBeltForeign = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("皮带异物")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("大块煤检测")))
	{
		dataInfo.extraAiDataInfo.m_nLargeCoal = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("大块煤检测")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("皮带跑偏算法")))
	{
		dataInfo.extraAiDataInfo.m_nBeltDeviation = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("皮带跑偏算法")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("烟雾火点")))
	{
		dataInfo.extraAiDataInfo.m_nSmokeFire = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("烟雾火点")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("抽烟打电话")))
	{
		dataInfo.extraAiDataInfo.m_nSmokeMakeCall = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("抽烟打电话")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("测温报警")))
	{
		dataInfo.extraAiDataInfo.m_nTemp = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("测温报警")].size();
	}
	if (dataInfo.extraAiDataInfo.m_mapExtraAiInfo.contains(A2T("周界防范")))
	{
		dataInfo.extraAiDataInfo.m_nPerimeter = dataInfo.extraAiDataInfo.m_mapExtraAiInfo[A2T("周界防范")].size();
	}

	//组包完成后，将生成报告的记录插入到数据库中
	{
		//去查询报警记录表
		QVector<QSqlRecord> vecInfo;
		QString sErrMsg = "";
		QString sPath = sOutPath.remove(sPdfFolderPath);
		QString sPdfPathUrl = sHttpPrefix + sPath;
		QString sRobPdfPathUrl = sPdfPathUrl.replace("#", "%23");//URL路径
		CSqlOper::instance().insertResult(createGuid(),1, sCurrTime + A2T("AI检测报告.pdf"), dtSearchEndTime, dataInfo.extraAiDataInfo.m_sOutputPdfFolder, sRobPdfPathUrl,sErrMsg);
	}
	return true;
}
